<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8" v-if="optType !='view'">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes:md:bom:add']"
        >新增
        </el-button>

      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mes:md:bom:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bomList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="产品编码" align="center" prop="code"/>
      <el-table-column label="价格" align="center" prop="price" :show-overflow-tooltip="true"/>
      <el-table-column label="产品属性" align="center" prop="attr_value" :show-overflow-tooltip="true"/>
      <el-table-column label="安全库存" width="90px" align="center" prop="safe_stock_flag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.safe_stock_flag"/>
        </template>
      </el-table-column>
      <el-table-column label="最低库存量" width="90px" align="center" prop="min_stock"/>
      <el-table-column label="最大库存量" width="90px" align="center" prop="max_stock"/>
      <el-table-column label="启用" width="60px" align="center" prop="enable_flag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.enable_flag"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="optType !='view'">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:md:bom:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:md:bom:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品BOM关系对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="物料名称" prop="name">
          <el-input v-model="form.name" readonly placeholder="请输入物料名称" disabled/>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input-number v-model="form.price" :precision="2" :step="0.1"></el-input-number>
        </el-form-item>
        <el-form-item label="产品属性" prop="attr_value">
          <template>
            <div>
              <el-select v-model="form.attr_name" placeholder="请选择">
                <el-option
                  v-for="item in goodsValues"
                  :key="item.values"
                  :label="item.values"
                  :value="item.values">
                </el-option>
              </el-select>
            </div>
          </template>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否启用">
              <el-radio-group v-model="form.enable_flag" disabled v-if="optType=='view'">
                <el-radio
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
              <el-radio-group v-model="form.enable_flag" v-else>
                <el-radio
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="安全库存">
              <el-radio-group v-model="form.safe_stock_flag" disabled v-if="optType=='view'">
                <el-radio
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>

              <el-radio-group v-model="form.safe_stock_flag" v-else>
                <el-radio
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="最低库存量" prop="min_stock">
          <el-input-number v-model="form.min_stock" :min="0"></el-input-number>
        </el-form-item>
        <el-form-item label="最大库存量" prop="min_stock">
          <el-input-number v-model="form.max_stock" :min="0"></el-input-number>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {getGoodsLineList, getGoodsLine, getGoodsValue, UpdateGoodsLine, InsertGoodsLine,delGoodsLine} from "@/api/zxbd/goods"

export default {
  name: "Bom",
  dicts: ["sys_yes_no"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      codes: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      flag: false,
      // 产品BOM关系表格数据
      bomList: [],
      goodsValues: {},
      goodsEcho: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        parent_code: this.parent_code,
        bomItemId: null,
        code: null,
        bomItemName: null,
        bomItemSpec: null,
        unitOfMeasure: null,
        quantity: null,
        enable_flag: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  props: {
    optType: undefined,
    parent_code: undefined,
    name: undefined
  },
  created() {
    this.getList();
    this.getGoodsValue();
  },
  methods: {
    getGoodsValue() {
      getGoodsValue().then(res => {
        this.goodsValues = res.data
      })
    },
    /** 查询产品BOM关系列表 */
    getList() {
      this.loading = true;
      getGoodsLineList(this.queryParams).then(response => {
        this.bomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name: this.name,
        code: null,
        parent_code: this.parent_code,
        attr_name: undefined,
        price: 0.00,
        enable_flag: "0",
        safe_stock_flag: "0",
        min_stock: 0,
        max_stock: 0,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.codes = selection.map(item => item.code)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.flag = true
      // this.$refs.AddIndex.showFlag = true;
      console.log(">>>>>>>>>>>>", this.form)
      this.open = true
      this.title = "添加商品扩展属性";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.flag = false
      const code = row.code || this.codes
      getGoodsLine(code).then(response => {
        this.form = response.data;
        this.goodsEcho = response.data.attr_value
        this.open = true;
        this.title = "修改商品扩展属性";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.flag) {
        this.submitAddForm()
      } else {
        this.$refs["form"].validate(valid => {
          if (valid) {
            UpdateGoodsLine(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        });
      }
    },

    /** 提交按钮 */
    submitAddForm() {
      InsertGoodsLine(this.form).then(response => {
        this.$modal.msgSuccess("添加成功");
        this.open = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log("这个row是",row)
      const codes = row.code || this.codes;
      this.$modal.confirm('是否确认删除商品扩展属性编号为"' + codes + '"的数据项？').then(function () {
        return delGoodsLine(codes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
  }
};
</script>
